import {Component, OnInit, ViewChild} from '@angular/core';
import { EntityService, enumBtnCode} from '../../../services/entity.service';
import {forEach} from '@angular/router/src/utils/collection';
import {NgForm} from '@angular/forms';
import {AppContextService} from '../../../services/app-context.service';
import {HandleParams} from '../../../utils';

@Component({
  selector: 'app-base-search',
  templateUrl: './base-search.component.html'
})
export class BaseSearchComponent implements OnInit {
  cars: any[];
  checked = false;

  model: any;
  columns: any[];
  params: any;
  yesNoArr: any;
  @ViewChild('searchForm') searchForm: NgForm;  // 查询表单
  ddConfig: any;

  constructor(
    private app: AppContextService,
    private entityService: EntityService,
  ) {}
  ngOnInit() {
    console.log(this.entityService.test);

    this.ddConfig = {
      placeholder: '--请选择--',
      style: {'width': '100%', 'min-width': '50px'}
    };
    this.yesNoArr = [ { value: '0', label: '否'}, { value: '1', label: '是'} ];

    this.cars = [
      {label: 'Audi', value: 'Audi'},
      {label: 'BMW', value: 'BMW'},
      {label: 'Fiat', value: 'Fiat'},
    ];
    this.model = this.entityService.pageData.search;
    this.columns = this.initColumns();
    this.params = this.entityService.params;
  }
  get dictionaries(){
    return this.entityService.dictionaries;
  }

  initColumns() {
    let res = [];
    const labelClass =  'ui-md-' + this.model.fieldLabel[0];
    const valueClass =  'ui-md-' + this.model.fieldLabel[1];
    if (typeof this.model.columnCount === 'number') {   //
      const columnClass = 'ui-md-' + (12 / this.model.columnCount);
      for (let i = 0; i < this.model.columnCount; i++) {
        let icolumn = { class: columnClass, labelClass: labelClass , valueClass: valueClass , columns: [], hasButton: false};
        for (let j = i; j < this.model.cols.length; j = j + this.model.columnCount) {
          icolumn.columns.push(this.model.cols[j]);
        }
        if (i === this.model.columnCount - 1) {
          icolumn.hasButton = true;
        }
        res.push(icolumn);
      }
    }
    if (typeof this.model.columnCount === 'string') {
      let columnCountArr = this.model.columnCount.split(',');
      columnCountArr.forEach( (layout, i) => {
        const columnClass = 'ui-md-' + (layout);
        let icolumn = { class: columnClass, labelClass: labelClass , valueClass: valueClass , columns: [], hasButton: false};
        for (let j = i; j < this.model.cols.length; j = j + columnCountArr.length) {
          icolumn.columns.push(this.model.cols[j]);
        }
        if (i === columnCountArr.length - 1) {
          icolumn.hasButton = true;
        }
        res.push(icolumn);
      });
    }
    return res;
  }

  btnClick(btn, item?) {
    btn = this.app.btnCodeToBtnObject(btn);

    switch (btn.functionCode) {
      case enumBtnCode[enumBtnCode.search]:  // 查询
        item = HandleParams(this.params, this.entityService.pageParams);
        this.entityService.btnClick(btn, item);
        break;

      case enumBtnCode[enumBtnCode.refresh]:  // 刷新
        this.params = this.entityService.params;
        item = HandleParams(this.params, this.entityService.pageParams);
        this.entityService.btnClick(btn, item);
        break;

      case enumBtnCode[enumBtnCode.clear]:    // 清空
        this.searchForm.reset();
        break;
    }
  }
}
